<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:fn="http://www.w3.org/2005/02/xpath-functions" 
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
xmlns:kml="http://www.opengis.net/kml/2.2"
xpath-default-namespace="http://www.opengis.net/kml/2.2"> <!-- remove pre: before production -->
	<xsl:output method="text" indent="yes" encoding="UTF-8" />
	<xsl:strip-space elements="*" /> <!-- Strip space from all elements, override with xsl:preserve-space if necessary -->
	<!-- Parameters -->
	<!-- TODO: create a function which takes a bind id and finds the associated 
		iText -->
	<!-- Global Variables -->
	<xsl:variable name="quot">"</xsl:variable>
	<xsl:variable name="comma">,</xsl:variable>
	<xsl:variable name="apos">'</xsl:variable>
	<xsl:variable name="newline">&#13;&#10;</xsl:variable>
	<xsl:variable name="slash">/</xsl:variable>
	<xsl:variable name="period">.</xsl:variable>

	<!--  Global Functions -->
	<!--
	getSpeed() 
	Return the speed for a given Path. The Path is represented as a Placemark
	with an included LineString node. The LineString contains the coordinate
	set for the Path. A "description" child of the Placemark contains the speed 
	 -->
	 <xsl:variable name="labelSpeed">speed</xsl:variable>
	 <xsl:variable name="equalSign">=</xsl:variable>
	 <xsl:variable name="minLen">
	 	<xsl:value-of select="string-length(concat($labelSpeed,$equalSign, '00'))">
	 	</xsl:value-of>
	 </xsl:variable>
	<!-- Templates -->
	<xsl:template match="/">
<xsl:text>#------------------------------------------------------------------------------
#
# An AUSI "CCL" file
# Generated by the program "kml2ccl" on Thu Jan 01 00:00:01 1970
# Edit at your own risk
#
# egrep -v '#|^ *$' will show active data lines only
#
#------------------------------------------------------------------------------
# 	major minor bugfix
version:    0:	  1: 	 0
#
# File information ------------------------------------------------------------
#
# time stamp: creation time string in epoch seconds
# lines: number of CCL statements
#
#                     time stamp            lines
info           :      1337265962:		</xsl:text><!-- Find out how to insert date here -->
<xsl:value-of select="count(kml/Document/Folder/Placemark)" />
<xsl:text>
#
# CCL statements--------------------------------------------------------------
#</xsl:text>
<xsl:apply-templates select="kml/Document/Folder" />
<xsl:text>
#
#------------------------------------------------------------------------------
#
# End of CCL file
#
#------------------------------------------------------------------------------
</xsl:text>
</xsl:template>

<xsl:template match="open" />
<xsl:template match="kml/Document/Folder">
	<xsl:for-each select="descendant::Placemark/LineString/coordinates">
		<xsl:variable name="desc-text" select="parent::*/preceding-sibling::description"/>
		<xsl:variable name="path-speed" select="substring(substring-after($desc-text, concat($labelSpeed,$equalSign)), 1, 2)"/>
		<xsl:variable name="tokenized-coords" select="tokenize(normalize-space(.), '\s+')"/>
		<xsl:for-each select="$tokenized-coords">			
	      		<xsl:text>&#13;&#10;</xsl:text>
			<xsl:text>REQUEST MSG_THREAD_NOT_USED REQUEST_NO_ACK QUEUE GOTO </xsl:text>
	      		<xsl:value-of select="substring(substring-before(substring-after(., $comma), $comma), 1, 10)"/>
	      		<xsl:text> </xsl:text>
	      		<xsl:value-of select="substring(substring-before(., $comma), 1, 11)"/>
	      		<xsl:text> </xsl:text>
	      		<xsl:value-of select="substring-after(substring-after(., $comma), $comma)"/>
	      		<xsl:text> </xsl:text>
			<xsl:value-of select="$path-speed"/>
		</xsl:for-each>
    </xsl:for-each>
	<xsl:for-each select="descendant::Placemark/Point/coordinates">
		<xsl:variable name="desc-text" select="parent::*/preceding-sibling::description"/>
		<xsl:variable name="path-speed" select="substring(substring-after($desc-text, concat($labelSpeed,$equalSign)), 1, 2)"/>
		<xsl:variable name="tokenized-coords" select="tokenize(normalize-space(.), '\s+')"/>
		<xsl:for-each select="$tokenized-coords">			
	      		<xsl:text>&#13;&#10;</xsl:text>
			<xsl:text>REQUEST MSG_THREAD_NOT_USED REQUEST_NO_ACK QUEUE GOTO </xsl:text>
	      		<xsl:value-of select="substring(substring-before(substring-after(., $comma), $comma), 1, 10)"/>
	      		<xsl:text> </xsl:text>
	      		<xsl:value-of select="substring(substring-before(., $comma), 1, 11)"/>
	      		<xsl:text> </xsl:text>
	      		<xsl:value-of select="substring-after(substring-after(., $comma), $comma)"/>
	      		<xsl:text> </xsl:text>
			<xsl:value-of select="$path-speed"/>
		</xsl:for-each>
    </xsl:for-each>
</xsl:template>
<xsl:template match="LookAt" />
<xsl:template match="visibility" />
<xsl:template match="styleUrl" />
<xsl:template match="Style" />
<xsl:template match="name"/>
<xsl:template match="description"/>
<xsl:template match="Point"/>
<xsl:template match="LineString"/>
<xsl:template match="Polygon"/>
<xsl:template match="extrude" />
<xsl:template match="tessellate" />
<xsl:template match="altitudeMode" />
<xsl:template match="outerBoundaryIs"/>
<xsl:template match="innerBoundaryIs"/>
<xsl:template match="LinearRing"/>
<xsl:template match="coordinates"/>

</xsl:stylesheet>
